package rs.belex.belexdroid.activities;

import rs.belex.belexdroid.R;
import rs.belex.belexdroid.beans.interfaces.User;
import rs.belex.belexdroid.services.impl.ksoap2.LoginKsoap2Impl;
import rs.belex.belexdroid.services.interfaces.ILogin;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class WelcomeActivity extends Activity {

	private User user;
	private String username, password;
	private EditText usernameEditText, passwordEditText;

	private static boolean rememberMe;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.welcome);

		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_login);
		final Button loginButton = (Button) findViewById(R.id.login_button);
		if (checkBox.isChecked()) {
			checkBox.setChecked(true);
		}

		SharedPreferences preferences = super.getPreferences(MODE_PRIVATE);
		username = preferences.getString("username", "");
		password = preferences.getString("password", "");
		rememberMe = preferences.getBoolean("rememberMe", false);

		usernameEditText = (EditText) findViewById(R.id.txt_username);
		passwordEditText = (EditText) findViewById(R.id.txt_password);
		if (username.length() != 0)
			usernameEditText.setText(username);
		if (password.length() != 0)
			passwordEditText.setText(password);
		checkBox.setChecked(rememberMe);

		loginButton.setOnClickListener(new OnClickListener() {
			public void onClick(View viewParam) {

				username = usernameEditText.getText().toString();
				password = passwordEditText.getText().toString();

				if (username.length() == 0 || password.length() == 0) {
					alertDialog.setTitle("Info");
					alertDialog
							.setMessage("Please provide username and password");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									return;
								}
							});
					alertDialog.show();
				} else {
					ILogin login = new LoginKsoap2Impl();
					user = login.login(username, password);
					if (user == null) {
						alertDialog.setTitle("Info");
						alertDialog.setMessage("Could not login, try again!");
						alertDialog.setButton("OK",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										return;
									}
								});
						alertDialog.show();
					} else {
						Intent i = new Intent(WelcomeActivity.this,
								GetStockQuote.class);
						String[] userData = new String[2];
						userData[0] = user.getUsername();
						userData[1] = user.getPassword();
						i.putExtra("userData", userData);
						startActivity(i);
					}
				}
			}
		}

		);

		checkBox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (((CheckBox) v).isChecked()) {
					rememberMe = true;
				} else {
					rememberMe = false;
				}
			}
		});
	}

	@Override
	protected void onStop() {

		super.onStop();
		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();

		editor.clear();

		editor.putBoolean("rememberMe", rememberMe);
		if (rememberMe) {
			editor.putString("username", username);
			editor.putString("password", password);
		}
		editor.commit();

	}
}
